//
//  GradeTableViewCell.m
//  GYWBaseAPP
//
//  Created by 郭艺伟 on 16/5/20.
//  Copyright © 2016年 郭艺伟. All rights reserved.
//

#import "GradeTableViewCell.h"

//#define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

//
///**
// *  随机颜色
// */
//#define RandomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]

#define kGradeLabelFontSize 18

#define kScoresLabelFontSize 12

#define kDescriptionLabelFontSize 12


#import <Masonry/Masonry.h>

@interface GradeTableViewCell ()

/**
 *  cell所有子视图的父视图
 */
@property (nonatomic, weak) UIView *cellBGView;

/**
 *  等级标题
 */
@property (nonatomic, strong) UILabel *gradeLabel;

/**
 *  分数标题
 */
@property (nonatomic, strong) UILabel *scoresLabel;

/**
 *  分割线
 */
@property (nonatomic, strong) UIView *verticalLineView;

/**
 *  描述标题
 */
@property (nonatomic, strong) UILabel *descriptionLabel;


/**
 *  分割线
 */
@property (nonatomic, strong) UIView *bottomLineView;




@end

@implementation GradeTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"GradeTableViewCell";
    GradeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[GradeTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        //1.设置cell的背景颜色背景图
        self.backgroundColor = [UIColor clearColor];
        self.backgroundView = nil;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        //2.添加所有子视图
        [self setupAllSupViews];
    }
    return self;
}

-(void)setupAllSupViews
{
    //1.cellBGView(cell所有子视图的父视图)
    UIView *cellBGView = [[UIView alloc] init];
    cellBGView.backgroundColor = [UIColor whiteColor];
    cellBGView.layer.masksToBounds = YES;
    [self.contentView addSubview:cellBGView];
    self.cellBGView = cellBGView;
    
    
    //1.等级标题label
    UILabel *gradeLabel = [[UILabel alloc] init];
    gradeLabel.backgroundColor = cellBGView.backgroundColor;
    //gradeLabel.backgroundColor = [UIColor redColor];
    gradeLabel.font = [UIFont systemFontOfSize:kGradeLabelFontSize];
    gradeLabel.textAlignment = NSTextAlignmentCenter;
    //gradeLabel.textColor = UIColorFromRGB(0xffb32e);
    [cellBGView addSubview:gradeLabel];
    _gradeLabel = gradeLabel;
    _gradeLabel.text = @"s";
    
    
    //2.分数标题label
    UILabel *scoresLabel = [[UILabel alloc] init];
    scoresLabel.backgroundColor = cellBGView.backgroundColor;
    //scoresLabel.backgroundColor = [UIColor yellowColor];
    scoresLabel.font = [UIFont systemFontOfSize:kScoresLabelFontSize];
    scoresLabel.textAlignment = NSTextAlignmentCenter;
    //scoresLabel.textColor = UIColorFromRGB(0xffb32e);
    [cellBGView addSubview:scoresLabel];
    _scoresLabel = scoresLabel;
    _scoresLabel.text = @"(100,110)";
    
    
    //3.分割线
    UIView *verticalLineView = [[UIView alloc] init];
    verticalLineView.backgroundColor = [UIColor greenColor];
    [cellBGView addSubview:verticalLineView];
    _verticalLineView = verticalLineView;
    
    //4.描述标题label
    UILabel *descriptionLabel = [[UILabel alloc] init];
    descriptionLabel.backgroundColor = cellBGView.backgroundColor;
    //descriptionLabel.backgroundColor = [UIColor redColor];
    descriptionLabel.font = [UIFont systemFontOfSize:kDescriptionLabelFontSize];
    descriptionLabel.textColor = UIColorFromRGB(0x666666);
    descriptionLabel.textAlignment = NSTextAlignmentCenter;
    [cellBGView addSubview:descriptionLabel];
    _descriptionLabel = descriptionLabel;
    _descriptionLabel.text = @"等级描述功能";
    
    
    [self.cellBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.gradeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.cellBGView.mas_centerY).offset(-2);
        make.left.equalTo(self.cellBGView.mas_left).offset(0);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(kGradeLabelFontSize+3);
    }];
    
    
    [self.scoresLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cellBGView.mas_centerY).offset(2);
        make.left.equalTo(self.cellBGView.mas_left).offset(0);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(kScoresLabelFontSize+3);
    }];
    
    [self.verticalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.cellBGView);
        make.left.equalTo(self.cellBGView).offset(80);
        make.width.mas_equalTo(1);
        make.bottom.equalTo(self.cellBGView);
        
    }];
    
    [self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cellBGView).with.offset(0);
        make.left.equalTo(self.verticalLineView.mas_right).offset(0);
        make.right.equalTo(self.cellBGView).with.offset(0);
        make.bottom.equalTo(self.cellBGView).offset(0);
    }];
    
    
    //底部增加一条线2016
    UIView *bottomLineView=[[UIView alloc]init];
    bottomLineView.backgroundColor=[UIColor colorWithWhite:0.5 alpha:0.5];
    [cellBGView addSubview:bottomLineView];
    _bottomLineView = bottomLineView;
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.cellBGView).with.offset(0);
        make.left.equalTo(self.cellBGView).offset(0);
        make.right.equalTo(self.cellBGView).with.offset(0);
        make.height.mas_equalTo(1);
        
    }];
    
}

-(void) setGrage:(GrageType)grage
{
    _grage = grage;
    
    switch (grage) {
        case GrageType_S: {
            _gradeLabel.textColor = UIColorFromRGB(0xffb32e);
            _scoresLabel.textColor = UIColorFromRGB(0xffb32e);
            break;
        }
        case GrageType_A: {
            _gradeLabel.textColor = UIColorFromRGB(0x8db0b2);
            _scoresLabel.textColor = UIColorFromRGB(0x8db0b2);
            break;
        }
        case GrageType_B: {
            _gradeLabel.textColor = UIColorFromRGB(0xba774a);
            _scoresLabel.textColor = UIColorFromRGB(0xba774a);
            break;
        }
        case GrageType_C: {
            _gradeLabel.textColor = UIColorFromRGB(0x999999);
            _scoresLabel.textColor = UIColorFromRGB(0x999999);
            break;
        }
        case GrageType_D: {
            _gradeLabel.textColor = UIColorFromRGB(0xb6b6b6);
            _scoresLabel.textColor = UIColorFromRGB(0xb6b6b6);
            break;
        }
    }
}

-(void)setFrame:(CGRect)frame
{
    CGRect newRect =CGRectMake(10, frame.origin.y, frame.size.width-2*10, frame.size.height);
    [super setFrame:newRect];
}



@end
